import requests
import re

def main():
    url = "http://127.0.0.1:11434/api/generate"  # 确保这是正确的API端点
    model = "deepseek-r1:7b"

    print("开始与Ollama模型对话。输入 'exit' 结束对话。")
    
    while True:
        prompt = input("你: ")
        
        if prompt.lower() in ['exit', 'quit']:
            print("结束对话。")
            break
        
        payload = {
            "model": model,
            "prompt": prompt,
            "stream": False
        }

        try:
            response = requests.post(url, json=payload)
            
            if response.ok:
                ollama_response = response.json().get('response', '未获取到回复')
                # 清理响应中的无用标签和多余换行符
                cleaned_response = ollama_response.replace("<think>", "").replace("</think>", "")
                # 使用正则表达式将多个换行符替换为单个换行符
                cleaned_response = re.sub(r'\n\s*\n', '\n\n', cleaned_response.strip())
                print(cleaned_response)
            else:
                print(f"请求失败，状态码：{response.status_code}")
                print(f"响应内容：{response.text}")
                
        except Exception as e:
            print(f"发生异常: {e}")

if __name__ == "__main__":
    main()